package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceCode;
import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.BrandCategoryMapper;
import cn.tedu.csmall.product.mapper.BrandMapper;
import cn.tedu.csmall.product.mapper.SpuMapper;
import cn.tedu.csmall.product.pojo.dto.BrandAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.BrandUpdateNewDTO;
import cn.tedu.csmall.product.pojo.entity.Brand;
import cn.tedu.csmall.product.pojo.vo.BrandStandardVO;
import cn.tedu.csmall.product.service.IBrandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class BrandServiceImpl implements IBrandService {
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private BrandCategoryMapper brandCategoryMapper;
    @Autowired
    private SpuMapper spuMapper;
    @Override
    public void addNew(BrandAddNewDTO brandAddNewDTO){
        log.debug("开始处理【添加品牌】的业务：{}", brandAddNewDTO);
        //检查品牌名称是否被占用
        int count = brandMapper.countByName(brandAddNewDTO.getName());
        if (count>0){
            String message = "添加品牌失败，品牌名称【"+brandAddNewDTO.getName()+"】已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);

        }

        System.out.println("brandAddNewDTO = " + brandAddNewDTO);
        Brand brand = new Brand();
        BeanUtils.copyProperties(brandAddNewDTO,brand);
        brand.setCommentCount(0);
        brand.setPositiveCommentCount(0);
        brand.setSales(0);
        brand.setProductCount(0);
        log.debug("准备插入品牌数据：{}", brand);
        int rows = brandMapper.insert(brand);
        if(rows!=1){
            String message = "添加品牌失败,服务器忙,请稍后再次尝试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }

    }

    @Override
    public void deleteById(Long id) {
        //调用mapper中的getStandardById 查看是否有对应id的数据
        //判断查询结果是否为null
        //是,此id对应的数据不存在 抛出异常
        //调用mapper的deleteById()执行删除,并获取返回值

        BrandStandardVO brandStandardVO = brandMapper.getStandardById(id);
        log.debug("品牌数据为{}",brandStandardVO);
        if(brandStandardVO==null){
            String message = "删除品牌失败,尝试删除的数据不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        //调用brandCategoryMapper的countByBrandId 方法统计关联的数量
        //如果大于零则不能删除
        int countByBrandId = brandCategoryMapper.countByBrandId(id);
        log.debug("类别中关联的数据数量为{}",countByBrandId);
        if (countByBrandId>0){
            String message = "删除品牌失败,存在关联的类别!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        //调用SpuMapper中的countByBrandId统计与Spu中关联的数据数量
        //如果大于零则不能删除
         countByBrandId = spuMapper.countByBrandId(id);
        log.debug("Spu中关联的数据数量为{}",countByBrandId);
        if (countByBrandId>0){
            String message = "删除品牌失败,存在关联的Spu!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        //判断以上返回值是否不为1
        //是 抛出异常
        int rows = brandMapper.deleteById(id);
        if (rows!=1){
            String message = "删除品牌失败,服务器忙,请稍后尝试";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }



    }

    @Override
    public void setEnable(Long id) {
        //根据id查询品牌是否存在
        BrandStandardVO brandStandardVO = brandMapper.getStandardById(id);
        //如果查询的数据为null则表示不存在 抛出异常
        if (brandStandardVO == null) {
            String message = "启用品牌失败,尝试启用的数据不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        //判断查询结果中的enable是否为1
        if(brandStandardVO.getEnable()==1){
            //是 ,此id的品牌已经是启用状态,则抛出异常
            String message ="此品牌已经是启用状态!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        //启用品牌 调用mapper的updateById
        Brand brand = new Brand();
        brand.setId(id);
        brand.setEnable(1);
        int rows = brandMapper.updateById(brand);
        //判断生效的行数是否不等于1 如果不等于1 则启用失败
        if(rows!=1){
            String message = "启用品牌失败,服务器忙,请稍后尝试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }

    }

    @Override
    public void setDisable(Long id) {
        //根据id查询品牌是否存在
        BrandStandardVO brandStandardVO = brandMapper.getStandardById(id);
        //如果查询的数据为null则表示不存在 抛出异常
        if (brandStandardVO == null) {
            String message = "启用品牌失败,尝试启用的数据不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        //判断查询结果中的enable是否为
        if(brandStandardVO.getEnable()==0){
            //是 ,此id的品牌已经是禁用状态,则抛出异常
            String message ="此品牌已经是禁用状态!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        // 禁用品牌 调用mapper的updateById
        Brand brand = new Brand();
        brand.setId(id);
        brand.setEnable(0);
        int rows = brandMapper.updateById(brand);
        //判断生效的行数是否不等于1 如果不等于1 则启用失败
        if(rows!=1){
            String message = "禁用品牌失败,服务器忙,请稍后尝试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }

    }

    @Override
    public void updateById(BrandUpdateNewDTO brandUpdateNewDTO) {
        log.debug("即将处理修改品牌,参数:{}",brandUpdateNewDTO);
        BrandStandardVO standardById = brandMapper.getStandardById(brandUpdateNewDTO.getId());
        log.debug("根据id查询到的品牌数据={}",standardById);
        if (standardById==null){
            String message = "修改失败,该数据不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        Brand brand = new Brand();
        BeanUtils.copyProperties(brandUpdateNewDTO,brand);
        int rows = brandMapper.updateById(brand);
        if (rows!=1){
            String message ="修改失败,服务器忙,请稍后重试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
    }
}

